package week10;

//*******************************************************************
//  MagazineList.java       Author: Lewis/Loftus
//
//  Represents a collection of magazines.
//*******************************************************************

import week8.Sorting;

import java.util.LinkedList;
import java.util.List;

public class MagazineList {
    private MagazineNode list;

    //----------------------------------------------------------------
    //  Sets up an initially empty list of magazines.
    //----------------------------------------------------------------
    public MagazineList() {
        list = null;
    }

    //----------------------------------------------------------------
    //  Creates a new MagazineNode object and adds it to the end of
    //  the linked list.
    //----------------------------------------------------------------
    public void add(Magazine mag) {
        MagazineNode node = new MagazineNode(mag);
        MagazineNode current;

        if (list == null)
            list = node;
        else {
            current = list;
            while (current.next != null)
                current = current.next;
            current.next = node;
        }
    }

    //----------------------------------------------------------------
    //  Returns this list of magazines as a string.
    //----------------------------------------------------------------
    public String toString() {
        String result = "";

        MagazineNode current = list;

        while (current != null) {
            result += current.magazine + "\n";
            current = current.next;
        }
        return result;
    }

    //*****************************************************************
    //  An inner class that represents a node in the magazine list.
    //  The public variables are accessed by the MagazineList class.
    //*****************************************************************
    protected class MagazineNode {
        public Magazine magazine;
        public MagazineNode next;

        //--------------------------------------------------------------
        //  Sets up the node
        //--------------------------------------------------------------
        public MagazineNode(Magazine mag) {
            magazine = mag;
            next = null;
        }

        public Magazine getMagazine() {
            return magazine;
        }
    }

    public MagazineNode insert(int index, Magazine newMagazine) {
        MagazineNode current = list;
        int j = 0;
        while (current != null && j < index - 2) {
            current = current.next;
            j++;
        }
        MagazineNode node = new MagazineNode(newMagazine);
        if (index==1) {
            node.next = current;
            list = node;
        }
        else {
            node.next = current.next;
            current.next = node;
        }
        return current;
    }

    public void delete(int index){
        MagazineNode current = list;
        int j=0;
        while(current!=null&&j<index-2){
            current=current.next;
            j++;
        }
        if (index==1)
        {
            list = current.next;
        }
        else
            current.next=current.next.next;
    }

//    public int getLength(Magazine mag){
//        int length = 0;
//        MagazineNode node = new MagazineNode(mag);
//        while(node!=null){
//            length++;
//            node = node.next;
//        }
//        return length;
//    }

    //  选择排序法
    public void SelectSort()
    {
        int temp = 0;
        List magazines = new LinkedList();
            // 分离链表
            while (list!=null) {          //  size.()返回链表长度
                magazines.add(temp,list.getMagazine());
                list = list.next;
                temp++;
            }
        temp = 0;//  重置变量
        Comparable[] list = new Comparable[magazines.size()];
        //  纳入数组
        while (temp<magazines.size())
        {
            list[temp] = (Comparable) magazines.get(temp);
            temp++;
        }
        Sorting.selectionSort(list); // 选择排序法
        for (Comparable com : list)
            System.out.println(com);
    }
}


